package com.ophiux.cs.application.consultBase.impl;

import com.ophiux.cs.application.consultBase.dto.GetDeptSortsRequest;
import com.ophiux.cs.application.consultBase.dto.GetDeptSortsResponse;
import com.ophiux.cs.application.consultBase.dto.GetDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.GetDoctorResponse;
import com.ophiux.cs.application.consultBase.service.IGetDeptSortsAppService;
import com.ophiux.cs.application.consultBase.service.IGetDoctorAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.domain.common.bean.OphiuxCode;
import com.ophiux.cs.domain.consultBase.model.entity.DeptSorts;
import com.ophiux.cs.domain.consultBase.model.entity.Doctor;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsParentResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsQueryObj;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsResult;
import com.ophiux.cs.domain.consultBase.repositories.IDeptSortsRepository;
import com.ophiux.cs.domain.consultBase.repositories.IDoctorRepository;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GetDeptSortsAppService implements IGetDeptSortsAppService {

    @Autowired
    IDeptSortsRepository deptSortsRepositories;


    @Override
    public Result<ResponsePage<GetDeptSortsResponse>> getDeptSorts(GetDeptSortsRequest dto) {
        ResponsePage<GetDeptSortsResponse> resp=new ResponsePage<GetDeptSortsResponse>();
        List<GetDeptSortsResponse> returnList = new ArrayList<GetDeptSortsResponse>();
        //验证参数是否允许为空
        if(!dto.isParamValid()) {
            return Result.fail(ResultEnum.requestParamFail.getCode(), ResultEnum.requestParamFail.getMsg());
        }
        try {
            DeptSortsQueryObj deptSorts = null;
            deptSorts = (DeptSortsQueryObj) ShiftUtils.objToNewObj(dto, DeptSortsQueryObj.class);
            try {
                //查找所有一级科室
                List<DeptSortsParentResult> deptSortsList=deptSortsRepositories.getParentDeptSorts(deptSorts);
                for (int i=0;i<deptSortsList.size();i++){
                    DeptSortsParentResult dspr=deptSortsList.get(i);
                    //查询二级科室
                    List<DeptSortsResult> dsList=deptSortsRepositories.getDeptSortsByParentId(dspr.getSORT_ID());
                    dspr.setSORT_CHILDS(dsList);
                }
                int count=deptSortsRepositories.getParentDeptSortsCount(deptSorts);
                returnList = ShiftUtils.objToNewObjList(deptSortsList, GetDeptSortsResponse.class);
                resp.initData(returnList,count,deptSorts);
            } catch (OphiuxRepositoryException e) {
                e.printStackTrace();
                return Result.fail(ResultEnum.requestException.getCode(), e.getExceptionMessage());
            }
        } catch (Exception e) {
            log.info("【getDeptSorts】接口异常：",e);
            e.printStackTrace();
            return Result.fail(AppResultCode.EXCEPTION, "程序异常");
        }
        return Result.success(resp, "查询分类成功");
    }
}
